home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 826 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.0 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.std.c++
  4. Subject: Guaranteeing addresses vs. optimization in basic_string
  5. Date: 22 Mar 1996 10:17:33 PST
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Mar22155031@gabi.gabi-soft.fr>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 22 Mar 1996 14:50:31 GMT
  11. Return-Path: <daemon@meeker.UCAR.EDU>
  12. Content-Length: 3101
  13. X-Lines: 55
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMVLvLEy4NqrwXLNJAQEBGQH/fivjsHf6J+3yB8hj7XfQRK8LtuFgZZi5
  16.     /EYPe/9a0/qGnVO5vqPh0JkPMKE0/Sks6GgcZ0JXmRpDb+sZwzXsdg==
  17.     =a22E
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Concerning the class basic_string in the draft standard library: it has
  21. just occurred to me that the function `reserve' is actually trying to do
  22. two separate jobs.
  23.  
  24. 1. Facilitate optimization.  When I call reserve, I am telling
  25. basic_string that I expect to generate a string of `n' characters, and
  26. that it should prepare for this.  Thus, in the classical implementation,
  27. the class will immediatly allocate a buffer large enough, so that it
  28. will not have to continuously reallocate each time I add a character.
  29. On the other hand, one could easily imagine fragmented schemes in which
  30. this function was a no-op.
  31.  
  32. 2. Guarantee internal addresses.  Although the wording in the current
  33. draft is a little vague in places, it seems clear that the intent is
  34. that calling reserve guarantee the validity of things like the return
  35. value of data() or at().  This implies that it can never be a no-op.
  36. Also, since there is no function to undo this application of reserve, it
  37. is still not clear (in the users minds, hopefully not in the final
  38. standard) just how long this guarantee holds.
  39.  
  40. My question is: has anyone considered splitting this functionality into
  41. two separate functions.  The exact effects of `reserve' would be
  42. implementation dependant, the intend being to allow the optimization of
  43. future memory allocation if this is relevant to the implementation.
  44. Following the lead of iostream, the second function could be called
  45. freeze, with a single parameter indicating whether the stream is being
  46. frozen or unfrozen (freeze on or off).  The internal addresses are
  47. guaranteed between the moment freeze is turned on and when it is turned
  48. off.  This gives the user absolute control.
  49.  
  50. In the case of freeze, I would also suggest that calling a non-const
  51. function on a frozen string result in an error (or undefined behavior,
  52. which I don't like, but is more in keeping with the philosophy
  53. underlying STL).  Note that in a fragmented implementation, freeze
  54. should probably trigger the copy into a fixed array, so that you can
  55. guarantee after that `&at( 0 ) == data()'.
  56.  
  57. Anyway, this is just a suggestion.  In implementing my own basic_string,
  58. I have noticed that there is a lot of extra code (which actually gets
  59. executed) simply to ensure the address guarantee from reserve, whereas I
  60. suspect that most of the time reserve is called, it is only the
  61. optimization that is needed (and when it is the address guarantee, even
  62. the guarantee of reserve is not really sufficient).
  63.  
  64. Finally, I will admit that I have not considered the implications of
  65. this (or anything else, for that matter) with regards to iterators.  I
  66. believe that the intent is that a `char*' may serve as an iterator with
  67. the classical implementation; in this case, any non-const function may
  68. invalidate the iterator (unless, under the current rules, reserve has
  69. been called).
  70. -- 
  71. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  72. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  73. Conseils en informatique industrielle --
  74.                             -- Beratung in industrieller Datenverarbeitung
  75. ---
  76. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  77.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  78.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  79.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  80.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  81. ]
  82.